<?php

/**
* AuthRegister.class.php
*
* Register user
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once("framework/AuthSingleton.php");
require_once("framework/config.php");
require_once("framework/PageModule.class.php");
require_once("HTML/QuickForm.php");
require_once("HTML/QuickForm/Renderer/Default.php");
require_once("HTML/Template/IT.php");

function isUserUnique( $user )
{

    $userList = AuthSingleton::instance()->listUsers();

    foreach( $userList as $value )
    {
        if ( $value['user'] == $user )
        {
            return false;
        }
    }    
    return true;
}

// {{{ AuthRegister

class AuthRegisterModule extends PageModule 
{
    // {{{ getContent()

    
    /**
    * getContent()
    *
    */

    function getContent()
    {
        
        trigger_error("ENTER AuthRegisterModule::getContent()", E_USER_NOTICE);

        $options = &PEAR::getStaticProperty('CONFIG_INI','TEMPLATES'); 
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
    
        $form = new HTML_QuickForm("AuthRegister", 'post', rtrim($global['service_url'],'/').'/index.php?cmd=AuthRegister');

        $statusTpl = new HTML_Template_IT($options["templates_dir"]);
        $statusTpl->loadTemplatefile("status.tpl.xhtml",FALSE,FALSE);

        $form->setDefaults(array('username' => ( isset($_GET['username']) ? $_GET['username'] : null ) ));

        $form->addElement('text','username','Login:');
        $form->addElement('password','password',_('Hasło:'));
        $form->addElement('password','passwordRepeat',_('Powtórz hasło:'));
        $form->addElement('submit','Login','Dodaj');
        
        $form->applyFilter('username', 'trim');
        $form->applyFilter('password', 'trim');
        $form->applyFilter('passwordRepeat', 'trim');
        $form->addRule('username', _('Podaj login.'), 'required');
        $form->addRule('username', _('Podaj email jako login.'), 'email');
        $form->addRule('password', _('Podaj hasło.'), 'required');
        $form->addRule('passwordRepeat', _('Powtórz hasło.'), 'required');
        $form->addRule(array('password','passwordRepeat'), _('Podane hasła nie są identyczne.'), 'compare','==');
        

        $form->registerRule('isuserunique', 'callback', 'isUserUnique');
        
        $form->addRule('username', _('Użytkownik już istnieje w bazie.'), 'isuserunique');

        if ($form->validate()) 
        {
        
            $data = $form->getSubmitValues(true);

            $err = AuthSingleton::instance()->adduser($data['username'],$data['password'],null);
            if ( !$err )
            {
                trigger_error("AuthRegisterModule::getContent()  Could not add user ".$err->getMessage(), E_USER_ERROR);
            }
    
            $statusTpl->setVariable("STATUS",_("Użytkownik dodany."));                                  
            return $statusTpl->get();

        }
        else
        {
 
            $renderer =& new HTML_QuickForm_Renderer_Default($options);
            $renderer -> setFormTemplate(file_get_contents($options["templates_dir"]."form.tpl.xhtml"));
            $renderer -> setElementTemplate(file_get_contents($options["templates_dir"]."formElement.tpl.xhtml"));
            $renderer -> setRequiredNoteTemplate(file_get_contents($options["templates_dir"]."formRequiredNote.tpl.xhtml"));
            $form -> setRequiredNote(null); 
            $form->accept($renderer);
            $statusTpl->setVariable("STATUS",_('Podaj login i hasło. Użyj działającego adresu e-mail jako login.'));
            return $statusTpl->get().$renderer->toHtml().$statusTpl->get(); 
        }




    }
    
    // }}}

}

// }}} 

?>
